package com.earthea.chatbot.login.controller;

import com.earthea.chatbot.common.entity.BaseRsp;
import com.earthea.chatbot.domain.LoginUser;
import com.earthea.chatbot.domain.User;
import com.earthea.chatbot.login.common.util.SecurityUtils;
import com.earthea.chatbot.login.entity.request.UpdateUserReq;
import com.earthea.chatbot.login.service.TokenService;
import com.earthea.chatbot.login.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 个人信息 业务处理
 */
@RestController
@RequestMapping("/api/profile")
public class ProfileController {
    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private BCryptPasswordEncoder encoder;

    /**
     * 个人信息
     */
    @GetMapping
    public BaseRsp profile() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        BaseRsp result = BaseRsp.success();
        result.setData(loginUser);
        return result;
    }

    /**
     * 修改用户
     */
    @PutMapping
    public BaseRsp updateProfile(@RequestBody UpdateUserReq updateUserReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotEmpty(updateUserReq.getUsername()) && userService.checkUserNameUnique(updateUserReq.getUsername()) != null) {
            return BaseRsp.failed("修改用户'" + updateUserReq.getUsername() + "'失败，用户名已存在");
        }
        if (StringUtils.isNotEmpty(updateUserReq.getEmail()) && userService.checkEmailUnique(updateUserReq.getEmail()) != null) {
            return BaseRsp.failed("修改用户'" + updateUserReq.getEmail() + "'失败，邮箱账号已存在");
        }

        User user = new User();
        user.setUserId(loginUser.getUserId());
        user.setUsername(updateUserReq.getUsername());
        user.setEmail(updateUserReq.getEmail());
        if (userService.update(user) > 0) {
            // 更新缓存用户信息
            loginUser.setUsername(updateUserReq.getUsername());
            loginUser.setEmail(updateUserReq.getEmail());
            tokenService.setLoginUser(loginUser);
            return BaseRsp.success();
        }
        return BaseRsp.failed("修改个人信息异常，请联系管理员");
    }

    /**
     * 重置密码
     */
    @PutMapping("/updatePwd")
    public BaseRsp updatePwd(@RequestBody Map<String, String> params) {
        String oldPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String password = loginUser.getPassword();
        if (!encoder.matches(oldPassword, password)) {
            return BaseRsp.failed("修改密码失败，旧密码错误");
        }
        if (encoder.matches(newPassword, password)) {
            return BaseRsp.failed("新密码不能与旧密码相同");
        }
        newPassword = encoder.encode(newPassword);
        User user = new User();
        user.setUserId(loginUser.getUserId());
        user.setPassword(newPassword);
        if (userService.update(user) > 0) {
            // 更新缓存用户密码
            loginUser.setPassword(newPassword);
            tokenService.setLoginUser(loginUser);
            return BaseRsp.success();
        }
        return BaseRsp.failed("修改密码异常，请联系管理员");
    }
}
